// ==UserScript==
// @name           Gmail Favicon Alerts 3
// @description    Alerts you to the status of your Gmail Inbox through distinct Favicons.
// @version        3.1.1a
// @date           2010-03-04
// @author         Peter Wooley
// @namespace      http://peterwooley.com
// @include        https://mail.google.com/mail*
// @include        http://mail.google.com/mail*
// @include        http://mail.google.com/a*
// @include        https://mail.google.com/a*
// ==/UserScript==

function addFunctionality(gfia_instance) {
	if(gfia_instance) {
		self = gfia_instance;
		self.icons = {
			chat: '',//chrome.extension.getURL('chat.png'),
		};
		gfia_instance.getUnreadCountIcon = function() {
			if(!this.searchElement)
				this.searchElement = this.getSearchElement();
			var unread = this.getUnreadCount();
			var inChat = this.getChat() && this.newChat();
			if(!unread) {
				return inChat? this.icons.chat : this.unreadImage.src;
			}
			port.postMessage({show:true});
			if(this.getUnreadCountDisplay()) {
				return this.drawUnreadCount(unread, inChat? self.getChatImage() : null).toDataURL('image/png');
			} else {
				return inChat? this.icons.chat : this.unreadImage.src;
			}
		}
		gfia_instance.newChat = function() {
			var title = self.title.innerHTML;
			for(var index in self.chatText) {
				var location = title.indexOf(self.chatText[index].value);
				if(self.chatText[index].chars + location == title.length) {
					return true;
				}
			}
			return false;
		}
		gfia_instance.newMail = function() { return this.searchElement? this.searchElement.textContent.match(/\((\d*)\)/): false; }
		gfia_instance.getUnreadCount = function() {
			matches = this.newMail();
			return matches ? matches[1] : false;
		}
		gfia_instance.getSearchElement = function() {
			var element;
			var nav = document.getElementById('canvas_frame').contentDocument.getElementsByClassName('n0');
			
			if(nav[0]) {
				var potential = nav[0];
				
				if(potential.className.indexOf('n0') !== -1) {
					element = potential;
				}
			}
			if(element) {
				element.parentElement.addEventListener('DOMSubtreeModified', this.poll, false);
				return element;
			} else {
				return null;
			}	
		}
		gfia_instance.getChatImage = function() {
			if(!this.chatImage) {
				this.chatImage = document.createElement('img');
				this.chatImage.src = this.icons.chat;
			}
			return this.chatImage;
		}
		gfia_instance.getUnreadImage = function() {
			if(!this.unreadImage) {
				this.unreadImage = document.createElement('img');
				this.unreadImage.src = this.icons.unread;
			}
			return this.unreadImage;
		}
		gfia_instance.stop = function() {
			self.enabled = false;
			if(self.title) {
				self.title.removeEventListener('DOMSubtreeModified', self.poll, false);
			}
			if(this.searchElement) {
				this.searchElement.removeEventListener('DOMSubtreeModified', this.poll, false);
			}
			self.forceUpdate();
		}
		gfia_instance.start = function() {
			self.enabled = true;
			if(self.title) {
				self.title.addEventListener('DOMSubtreeModified', self.poll, false);
			}
			this.searchElement = this.getSearchElement();
			self.forceUpdate();
		}
	}
}